package dao

import (
	"database/sql"
	errors2 "errors"
	"fmt"
	"week2/utils"
)

/*****************dao层查询模拟*******************/

func QueryUserInfo(userId string) (*UserInfo, DaoError) {
	// 创建数据库与连接
	SQLs := utils.SQLConnecting{
		UserName:  utils.GlobalConfig.Sqlusername,
		Password:  utils.GlobalConfig.Sqlpassword,
		IPAddrees: utils.GlobalConfig.Sqladdr,
		Port:      utils.GlobalConfig.Sqlport,
		DbName:    utils.GlobalConfig.SqldbName,
		Charset:   utils.GlobalConfig.Sqlcharset}
	SQLC, err := utils.CreatSQLClient("mysql", SQLs)
	if err != nil {
		return nil, &daoError{500, "creak client err", err}
	}

	// sql 查询
	sqlString := fmt.Sprintf("select userId,name from user where userId='%s' and deleteStatus='No'", userId)
	var userInfo UserInfo
	err = SQLC.QueryStruct(&userInfo, sqlString)
	if errors2.Is(err, sql.ErrNoRows) {
		return nil, &daoError{100, "not fond", err}
	} else if err != nil {
		return nil, &daoError{500, "Sql Query err ", err}
	} else {
		return &userInfo, nil
	}
}

// UserInfo 用户基本信息
type UserInfo struct {
	UserID string
	Name   string
	// 其它数据库对应属性字段
	// ....
}
